import requests, re, time
from bs4 import BeautifulSoup

"""
<div class="entry-top">
	<a class="entry-thumbnail" href="https://www.kanxiaojiejie.com/i/9005" target="_blank">
	<img fifu-featured="1" width="520" src="https://t1.ledchuzu.com/2022/05/16/9e00d4eb30e28f7701e1c83fbd03ec31.jpg" class="attachment-gridzone-medium-h size-gridzone-medium-h wp-post-image" alt="20220516-5" title="20220516-5" title="20220516-5" loading="lazy" />																			</a>
	<div class="entry-category"><a href="https://www.kanxiaojiejie.com/i/category/xiaoxinggan" rel="category tag">小性感</a> <a href="https://www.kanxiaojiejie.com/i/category/xiaotianmei" rel="category tag">小甜美</a></div>
</div>
"""
# https://www.kanxiaojiejie.com/i/category/xiaoxinggan/page/6
# https://www.kanxiaojiejie.com/page/3
url = 'https://www.kanxiaojiejie.com/i/category/xiaoxinggan/page/21'  # page/2 可以更换  页面

resp = requests.get(url)

# 把源码交给bs
main_page = BeautifulSoup(resp.text, 'html.parser')
divlist = main_page.find_all('div', class_='entry-top')
imgs = []
for i in divlist:
    img = i.find_all('img')
    # print(img)
    imgs.append(img)

# <img alt="20220516-5" class="attachment-gridzone-medium-h size-gridzone-medium-h wp-post-image"
# fifu-featured="1" loading="lazy" src="https://t1.ledchuzu.com/2022/05/16/9e00d4eb30e28f7701e1c83fbd03ec31.jpg"
# title="20220516-5" width="520">
for i in imgs:
    obj = re.compile(f'.*?src="(?P<src>.*?)"', re.S)
    src_list = obj.finditer(str(i))
    # print(src_list.__next__())
    for j in src_list:
        src_dz = j.group('src')
        name = src_dz.split('/')[-1]  # 获取图片的最后当作名字
        # 下载图片
        src_rep = requests.get(src_dz)
        src_cont = src_rep.content  # 这里获取到的是字节
        with open('img/123/'+name, 'ab') as f:
            f.write(src_cont)
        print(name, 'ok')
        time.sleep(1)  # 防止服务器
print('OVER')